﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;

namespace SDLITS.Web.Customer
{
    public partial class CompanyManaList : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("CompanyChange.aspx", "新增企业");
                BindGrid(GetWhere());
            }
        }
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = SDDbSession.Default.From<V_CompanyARU>().Where(wc).Count();
            List<V_CompanyARU> items = SDDbSession.Default.From<V_CompanyARU>().Where(wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).OrderBy(V_CompanyARU._.CreateDate.Desc).ToList();
            Grid1.DataSource = items;
            Grid1.DataBind();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }
        protected WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!string.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && V_CompanyARU._.CompanyName.Contain(ttbxMyBox2.Text.Trim());
            }
            return wc;
        }
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                Grid1.PageIndex = 0;
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            Grid1.PageIndex = 0;
            BindGrid(GetWhere());
        }
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                SDDbSession.Default.Delete<Company>(Company._.CompanyID == id);
                BindGrid(GetWhere());
            }
            if (e.CommandName == "jh")
            {
                SDDbSession.Default.Update<Company>(Company._.StatusID, 1, Company._.CompanyID == id);
                BindGrid(GetWhere());
            }
            if (e.CommandName == "dj")
            {
                SDDbSession.Default.Update<Company>(Company._.StatusID, 2, Company._.CompanyID == id);
                BindGrid(GetWhere());
            }
            if (e.CommandName == "zx")
            {
                SDDbSession.Default.Update<Company>(Company._.StatusID, 3, Company._.CompanyID == id);
                BindGrid(GetWhere());
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[3] = Methed.Condition(e.Values[3].ToString());
            e.Values[1] = e.Values[1].ToString() + "—" + e.Values[9].ToString();
        }
    }
}